<?php

$objXajax->registerFunction("cusfrmpay_loadGrid");
$objXajax->registerFunction("cusfrmpay_edit");
$objXajax->registerFunction("cusfrmpay_delete");
$objXajax->registerFunction("cusfrmpay_save");
$objXajax->registerFunction("cusfrmpay_init");
    
function cusfrmpay_loadGrid(){     

    global $lang;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $arrGrid = cusfrmpay_getGrid();
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/customer/cus_pay_grid.html');
    $oTbs->MergeBlock('blk_data', $arrGrid);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('cusfrmpay_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('cusfrmpay_tbl');");
    $objResponse->addScript('changeWaiting(1);');                              
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function  cusfrmpay_save($params){
    global $objDbSelect,$user_id,$tbl_postfix,$hotel_id;
    $objResponse = new xajaxResponse();
    $money = $params['money'] ;
    $date = $params['date'] ;
    $flg = $params['cbbtype'] ;
    $description  = addslashes(strip_tags($params['description']));
    $edit = $params['hdEdit'];
    $id= $params['hdId'];
    if($edit==1){
        $sql = "UPDATE cus_pay SET money='$money',date_pay='$date',description='$description',date_update=now(),user_update='$user_id',flg='$flg' WHERE id ='$id' and service_id=$hotel_id ";
  
    }else{
         $sql = "INSERT INTO cus_pay(money,date_pay,description,date_created,user_created,date_update,user_update,flg,service_id) 
                                value('$money','$date','$description',now(),'$user_id',now(),'$user_id',$flg,$hotel_id)";   
    }

    if($objDbSelect->Execute($sql)){
       $objResponse->addScript("checkUpdateStatus(1,'cusfrmpay','$edit');");
       $objResponse->addScript('xajax_cusfrmpay_loadGrid();'); 
        }else{
       $objResponse->addScript("checkUpdateStatus(0,'cusfrmpay','$edit');");
    }
    
    //$objResponse->addScript('changeWaiting(1);');
    return $objResponse->getXML(); 
} 
  
function  cusfrmpay_getGrid(){
    global $objDbSelect,$lang,$tbl_postfix,$user_id,$hotel_id;
    $sqlSelect = " SELECT *,datediff(now(),date_created) as today 
    FROM cus_pay  WHERE  datediff(now(),date_created)<30 and
     user_created = $user_id and service_id = $hotel_id order by date_created desc";
    return $objDbSelect->GetArray($sqlSelect);
} 
function  cusfrmpay_delete($id){
    global $objDbSelect,$lang,$tbl_postfix,$user_id,$hotel_id;
    $objResponse = new xajaxResponse();
    $sql = "delete from cus_pay where id = $id and service_id = $hotel_id and datediff(date_created,now())=0";
    $objDbSelect->Execute($sql);
    $objResponse->addScript('xajax_cusfrmpay_loadGrid();');
    $objResponse->addScript('changeWaiting(1);'); 
    return $objResponse->getXML();
}  

function  cusfrmpay_edit($id){
    global $objDbSelect,$tbl_postfix,$hotel_id;
    $sql = "SELECT date_format(date_pay,'%d/%m/%Y') date_pay,flg,money,description
     FROM cus_pay WHERE id = '$id' and service_id = $hotel_id";
    $arr = $objDbSelect->GetArray($sql);
    $objResponse = new xajaxResponse();
    $date = $arr[0]['date_pay'];
    $money =FormatMoney($arr[0]['money']) ;
    $flg = $arr[0]['flg'];
    
    $description =htmlspecialchars_decode($arr[0]['description']) ;
    $objResponse->addAssign('cusfrmpay_description','value',$description);
    
    $objResponse->addScript("$('#cusfrmpay_date').val('$date');");
    $objResponse->addScript("$('#cusfrmpay_money').val('$money');");
    $objResponse->addScript("$('#cusfrmpay_cbbtype').val('$flg');");

    $objResponse->addScript('changeWaiting(1);');
    return $objResponse->getXML();
} 

function  cusfrmpay_init(){
   $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax      
   $todate = SYSTEM_DATE;     
   $objResponse->addScript("$('#cusfrmpay_date').val('$todate')");     
   return $objResponse->getXML(); // dong luon luon co khi goi ham ajax    
}


# end region process data
?>
